package wortubung.inflection;

import java.util.ArrayList;
import java.util.HashMap;

import wortubung.core.LANG;

import static wortubung.util.CommonUtils.maxLen;

public enum CONJUGATION {
	            // de                       fr                      es                                 it                        la
	PRESENT       ("Präsens",               "Présent",              "Presente",                        "Presente",               "Present"               ),
	IMPERFECT     (null,                    "Imparfait",            "Pretérito Imperfecto",            "Imperfetto",             "Imperfetto"            ),
	SIMPLE_PAST   ("Präteritum",            "Passé Simple",         "Pretérito Perfecto Simple",       "Passato Remoto",         null                    ),
	FUTURE        (null,                    "Futur",                "Futuro",                          "Futuro",                 "Futuro Semplice"       ),
	SUBJ_PRESENT  ("Präsens Konjunktiv",    "Subjonctif Présent",   "Subjuntivo Presente",             "Congiuntivo Presente",   "Congiuntivo Presente"  ),
	SUBJ_IMPERFECT(null,                    "Subjonctif Imparfait", "Subjuntivo Pretérito Imperfecto", "Congiuntivo Imperfetto", "Congiuntivo Imperfetto"),
	SUBJ_PAST     ("Präteritum Konjunktiv", null,                   null,                              null,                     null                    ),
	SUBJ_FUTURE   ("Präteritum Konjunktiv", null,                   "Subjuntivo Futuro",               null,                     null                    ),
	CONDITIONAL   (null,                    "Conditionnel",         "Condicional",                     "Condizionale",           null                    ),
	IMPERATIVE    ("Imperativ",             "Impératif",            "Imperativo",                      "Imperativo",             null                    );

	private String[] indigenousNames;
	private static HashMap<LANG, CONJUGATION[]> perLanguage;

	static {
		perLanguage = new HashMap<LANG,CONJUGATION[]>();
		for (LANG lang : LANG.values()) {
			if (getLangIndex(lang) < 0)
				continue;
			ArrayList<CONJUGATION> ret = new ArrayList<CONJUGATION>();
			for (CONJUGATION c : values())
				if (c.getDisplay(lang) != null)
					ret.add(c);
			perLanguage.put(lang, ret.toArray(new CONJUGATION[ret.size()]));
		}
	}

	private static int getLangIndex(LANG lang) {
		switch(lang) {
		case de: return 0;
		case fr: return 1;
		case es: return 2;
		case it: return 3;
		case la: return 4;
		default: return -1;
		}
	}

	public static CONJUGATION[] forLanguage(LANG lang) {
		return perLanguage.get(lang);
	}
	
	private CONJUGATION(String... indigenousNames) {
		this.indigenousNames = indigenousNames;
	}

	public String getDisplay(LANG lang) {
		return indigenousNames[getLangIndex(lang)];
	}

	public int getDisplayWidth(LANG lang) {
		String s = getDisplay(lang);
		if (s == null)
			return 0;
		return maxLen(0, s.split(" +"));
	}

	public static int getDisplayMaxLayer(LANG lang) {
		int ret = 1;
		for (CONJUGATION c : CONJUGATION.forLanguage(lang)) {
			int x = c.getDisplay(lang).split(" +").length;
			if (x > ret)
				ret = x;
		}
		return ret;
	}

	public static String getInfinitiveDisplay(LANG lang) {
		switch(lang) {
		case fr: return "Infinitif";
		case de: return "Infinitiv";
		case es: return "Infinitivo";
		case it: return "Infinito";
		default: return null;
		}
	}

	public static String getParticiplesDisplay(LANG lang) {
		switch(lang) {
		case fr: return "Participes";
		case de: return "Partizipien";
		case es: return "Participios";
		case it: return "Partici";
		default: return null;
		}
	}

	public static int maxNameLength(LANG lang) {
		int ret = 9;
		for (CONJUGATION type : forLanguage(lang))
			if (type.name().length() > ret)
				ret = type.name().length();
		return ret;
	}
}
